package com.elink.enginerring.video.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elink.enginerring.common.BaseCo;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.common.Pagination;
import com.elink.enginerring.util.JSONUtil;
import com.elink.enginerring.video.dao.SkillFileMapper;
import com.elink.enginerring.video.dao.SkillMapper;
import com.elink.enginerring.video.dao.SkilltopCategoryMapper;
import com.elink.enginerring.video.entity.Skill;
import com.elink.enginerring.video.entity.SkillFile;
@Service
public class SkillService  implements ISkillService{
	@Autowired
	private SkillMapper skillMapper;
	@Autowired
	private SkillFileMapper skillFileMapper;
	@Autowired
	private SkilltopCategoryMapper skilltopCategoryMapper;

	@Override
	public Map<String, Object> getList(BaseCo co) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", skillMapper.selectList(co));
		map.put("pagination", new Pagination(co, skillMapper.selectCount(co)));
		return JSONUtil.getJSON(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> addFile(SkillFile skillFile) {
		//往skill里面更新文件数
		Skill skill = skillMapper.selectByPrimaryKey(skillFile.getSkillId());
		if(skill != null){
			skill.setDataCount(skill.getDataCount()+1);
			skillMapper.updateByPrimaryKeySelective(skill);
		}
		skillFileMapper.insertSelective(skillFile);
		
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> addSkill(Skill skill) {
		skill.setCreateTime(new Date());
		skill.setAuthStatus(1);//后台添加体系不需要审核
		skillMapper.insertSelective(skill);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> getCategory() {
		return JSONUtil.getJSON(ErrorType.SUCCESS,skilltopCategoryMapper.select());
	}
}
